﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using ca2simulator.shared;
using System.Windows.Media;
using System.Windows.Controls.Primitives;
using System.Windows.Media.Imaging;
using System.Windows;

namespace ca2simulator.client.view.grid
{
    public class LatticeDataGrid
    {
        private const float DPI = 100;

        private Lattice _Lattice;
        private GridBitmap _GridBitmap;
        
        
        public Lattice Lattice { get { return _Lattice; } }

        public LatticeDataGrid(Lattice lattice, Image image)
        {
            _Lattice = lattice;
            _GridBitmap = new GridBitmap(Lattice.Rows, Lattice.Columns, 10, 10);
            
            image.Source = _GridBitmap.Bitmap;
        }

        public void update()
        {
            Color color = Colors.Violet;
            for (int r = 0; r < Lattice.Rows; r++)
            {
                for (int c = 0; c < Lattice.Columns; c++)
                {
                    if (Lattice.Cells[r, c].CellState.State == 1)
                    {
                        color = Colors.Purple;
                    }
                    else color = Colors.White;
                    _GridBitmap.setCell(r, c, color);
                }
            }
            
        }
    }

    public class GridBitmap
    {
        private const float DPI = 100;
        private readonly int Rows, Cols, CellW, CellH;
        private Int32Rect _CellRect;

        private WriteableBitmap _Bitmap;
        public WriteableBitmap Bitmap { get { return _Bitmap; } }

        public GridBitmap(int rows, int col, int cellW, int cellH)
        {
            int bitWidth = rows * cellH;
            int bitHeight = col * cellW;
            Rows = rows; Cols = col;
            CellW = cellW; CellH = cellH;
            _CellRect = new Int32Rect(0, 0, CellW, CellH);
            _Bitmap = new WriteableBitmap(bitWidth, bitHeight, DPI, DPI, PixelFormats.Rgb24, null);
        }

        public void setCell(int row, int col,  Color color)
        {
            // Make sure no one else can modify bitmap
            _Bitmap.Lock();


            _CellRect.X = col*CellW;
            _CellRect.Y = row*CellH;
            {
                IntPtr buff = _Bitmap.BackBuffer;
                int stride = _Bitmap.BackBufferStride;
                unsafe
                {
                    byte* pbuff = (byte*)buff.ToPointer();
                    pbuff += stride * _CellRect.Y;
                    pbuff += _CellRect.X * 3;
                    for (int y = 0; y < CellH; y++)
                    {
                        for (int x = 0; x < CellW; x++)
                        {
                            pbuff[0] = color.B;
                            pbuff[1] = color.G;
                            pbuff[2] = color.R;

                            //move pointer
                            pbuff += 3;
                        }
                        //Move it back to the beginning of the block
                        pbuff -= 3 * CellW;
                        // Move it to the next row
                        pbuff += stride;
                    }
                }
            }
            _Bitmap.AddDirtyRect(new Int32Rect(0, 0, Cols * CellW, Rows * CellH));
            _Bitmap.Unlock();
            
            
        }

        
    }
}
